package com.inspur.edp.common.expr.visitor.field;

import com.inspur.edp.common.expr.exception.ExpressException;
import com.inspur.edp.common.expr.spi.FieldVisitor;
import com.inspur.edp.common.type.Field;
import io.iec.edp.caf.commons.utils.SpringBeanUtils;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author lizhaorui
 * @description TODO
 * @date 2024/9/27 13:55
 */
public class FieldAccessFactory {

    private final static Object lockObject = new Object();

    private static Map<String, FieldVisitor> PROVIDER_MAP;

    public static FieldVisitor getSpi(String type) {
        if (PROVIDER_MAP == null) {
            synchronized (lockObject) {
                if (PROVIDER_MAP == null) {
                    ConcurrentHashMap<String, FieldVisitor> temp = new ConcurrentHashMap<>();
                    //固定的几个
                    temp.put(Field.GET_SET_VALUE, new GetSetFieldVisitor());
                    temp.put(PublicFieldVisitor.KIND, new PublicFieldVisitor());
                    temp.put(JsonFieldVisitor.KIND, new JsonFieldVisitor());
                    temp.put(HashMapFieldVisitor.KIND, new HashMapFieldVisitor());
                    PROVIDER_MAP = temp;
                }
            }
        }
        FieldVisitor genSpi = PROVIDER_MAP.get(type);
        if (genSpi != null) {
            return genSpi;
        } else {
            throw new ExpressException("not find field access provider " + type);
        }
    }

}
